package com.tj.collection.aop;

import com.fqgj.log.util.Constants;
import com.robert.vesta.util.IdUtils;
import org.apache.logging.log4j.ThreadContext;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * task 生成traceId
 * Created with IntelliJ IDEA.
 * User: pavan
 * Date: 2019/01/02
 * Time: 下午3:47
 */
@Aspect
@Component
@Order(Integer.MAX_VALUE)
public class TaskTraceIdAop {

    @Around("execution(* com.tj.collection.task.*.execute(..))")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        Object resultObj = null;
        ThreadContext.put(Constants.TRACE_ID, IdUtils.genId() + "");
        try {
            resultObj = joinPoint.proceed(args);
            return resultObj;
        } catch (Exception e) {
            throw e;
        } finally {
            ThreadContext.clearAll();
        }
    }

}
